#!/usr/bin/python
# -*- coding:utf-8 -*-

import pym_log as log
import ctypes
import sys

ll = ctypes.cdll.LoadLibrary
lib = ll("../C/libsharemem.so")

#desp		:	write string into share mem
#parms		:	write_string,the string you want write
#return		:	0 success, 1 error
def test_write_share_memory(write_string):
	try:
		print write_string
		write_string_len=len(write_string)
		memsize=ctypes.c_int(write_string_len)

		#1. init share mem
		lib.init_share_mem.argtypes=[ctypes.c_int]
		ret_val=lib.init_share_mem(memsize)
		if ret_val !=0:
			log.error( "init_share_mem value:%r",ret_val)
			log.error("memsize:%r",memsize)
			return 1


		#2. write string
		log.debug("write string:%r,type:%r",write_string,type(write_string))
		p_str =  ctypes.create_string_buffer( write_string, len(write_string)+1)
		lib.write_share_mem.argtypes=[ctypes.c_char_p, ctypes.c_int]
		ret_val=lib.write_share_mem(p_str, write_string_len+1)
		if ret_val !=0:
			log.error("write share memory value:%r",ret_val)
			return 1

		pass

	except Exception,Ex:
		print Exception,":",Ex

if __name__=='__main__':
	log.set_logger(level = 'DEBUG:INFO',filename = 'write_mem.log', mode = 'w')
	write_string="李洪志\n法轮功"
	#write_string="aadfaffbcde"
	if len(sys.argv)>1:
		write_string=sys.argv[1]
	print write_string
	log.debug("write string:%r,type:%r",write_string,type(write_string))
	test_write_share_memory(write_string)
